<?php
namespace app\admin\controller;

use app\common\model\index\NavModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\facade\Validate;

/**
 * 首页导航
 * @package app\admin\controller
 * @author wangqinghui
 * @DateTime 2024-09-11 13:58:44
 */
class Nav extends Controller
{
    public $check_user    = false;
    public $check_account = false;

    /**
     * 首页导航
     * @auth true
     * @menu true
     * @author wangqinghui
     * @DateTime 2024-09-11 14:56:28
     * @return void
     */
    public function index()
    {
        NavModel::mQuery()->layTable(function () {
            $this->title = '导航管理';
            // 获取状态集合
            $this->statusArr = NavModel::getStatusArr();
        }, function (QueryHelper $query) {

            // 数据列表搜索过滤
            $query->like('name')->equal('status')->dateBetween('create_at');
            $query->order('sort desc, id asc');
        });
    }

    /**
     * 处理列表数据
     * @author wangqinghui
     * @DateTime 2024-09-11 16:32:38
     * @param [type] $data
     * @return void
     */
    protected function _index_page_filter(&$data){
        // 精准回调对 $data 进行二次处理，注意是引用
        $data = DataExtend::arr2tree($data);
        // 菜单数据树数据变平化
        $data = DataExtend::arr2table($data);
    }

    /**
     * 添加导航
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-11 15:52:48
     * @return void
     */
    public function add()
    {
        NavModel::mForm('form');
    }

    /**
     * 编辑导航
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-11 15:53:06
     * @return void
     */
    public function edit()
    {
        NavModel::mForm('form');
    }

    /**
     * 处理表单数据
     * @author wangqinghui
     * @DateTime 2024-09-11 15:53:21
     * @param array $vo
     * @return void
     */
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $rule = [
                'pid|上级导航' => 'require|integer',
                'name|导航名称' => 'require|length:1,20',
                'link|导航链接' => 'require|length:1,255',
                'intro|导航简介' => 'length:0,250',
                'id|导航ID'   => 'integer',
            ];
            $validate = Validate::rule($rule);
            if (!$validate->check($params)) {
                $this->error($validate->getError());
            }
        } else {
            /* 列出可选上级菜单 */
            $vo['pid'] = $vo['pid'] ?? $this->request->param('pid', '0');
            $menus = NavModel::mk()->order('sort desc,id asc')->column('id,pid,name,link,intro', 'id');
            $this->menus = DataExtend::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'link' => '#', 'name' => '顶部导航']]));
            if (isset($vo['id'])){
                foreach ($this->menus as $menu) {
                    if ($menu['id'] === $vo['id']) {
                        $vo = $menu;
                    }
                }
            }
            foreach ($this->menus as $key => $menu) {
                // if ($menu['spt'] >= 3 || $menu['link'] !== '#') {
                if ($menu['spt'] >= 3) {
                    unset($this->menus[$key]);
                }
            }
            if (isset($vo['spt']) && isset($vo['spc']) && in_array($vo['spt'], [1, 2]) && $vo['spc'] > 0) {
                foreach ($this->menus as $key => $menu){
                    if ($vo['spt'] <= $menu['spt']) {
                        unset($this->menus[$key]);
                    }
                }
            }
        }
    }

    /**
     * 修改状态
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-11 15:53:45
     * @return void
     */
    public function state()
    {
        $id = $this->request->post('id', 0);
        if(empty($id) || ($id <= 0)){
            $this->error('参数错误');
        }
        NavModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]), 'id', [
            'id'    => $id
        ]);
    }

    /**
     * 删除导航
     * @auth true
     * @author wangqinghui
     * @DateTime 2024-09-11 15:54:31
     * @return void
     */
    public function remove()
    {
        $id = $this->request->post('id', 0);
        if(empty($id) || ($id <= 0)){
            $this->error('参数错误');
        }
        NavModel::mDelete('id', [
            'id'    => $id
        ]);
    }
}